// 定义组合式api
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

const useTodoStore = defineStore('todo', () => {
  let todos = ref([
    { id: 1, title: '吃饭' },
    { id: 2, title: '睡觉' },
    { id: 3, title: '打豆豆' },
  ])

  let arr = [1, 2, 3, 4, 5]

  const total = computed(() => arr.reduce((pre, next) => pre + next, 0))

  // 务必要返回一个对象：属性与方法可以提供给组件使用
  return {
    todos,
    total,
    updateTodo() {
      todos.value.push({ id: 4, title: 'abc' })
    }
  }
})

export default useTodoStore